package cn.tedu.test.luban.rpc.client;

import cn.tedu.test.luban.rpc.HelloService;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HelloServiceProxy implements HelloService {
    @Override
    public String sayHi(String name) {
        try {
            //发送一个socket 进行一次通信
            Socket socket = new Socket("localhost", 20000);
            //准备传输的数据
            OutputStream outputStream = socket.getOutputStream();
            //高级流包低级流
            DataOutputStream dos = new DataOutputStream(outputStream);
            //数据传输 序列化 write 流 直接调用api方法
            dos.writeUTF(name);
            dos.flush();
            //对方给我有反馈
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            String result = dis.readUTF();
            System.out.println("客户端接收到服务端反馈:" + result);
            return result;
        }catch (Exception e){
            return null;
        }
    }
}
